#ifndef SIGNALDATA_H
#define SIGNALDATA_H

#include <QList>
#include <QColor>
#include <QScriptProgram>
#include <QMutex>
#include <QMutexLocker>

template <typename T>
class SignalData : public QObject
{
public:
    typedef T value_type;
    typedef QList<T> container_type;

    SignalData(QColor col = Qt::gray) : program("0"), color(col),
        xOffset(0), yOffset(0), xScale(0), yScale(0),
        bEnabled(false), bStopped(false)
    {
        m_container.reserve(1024);
    }

    SignalData(const SignalData & sd)  : program(sd.program), color(sd.color),
        xOffset(sd.xOffset), yOffset(sd.yOffset), xScale(sd.xScale), yScale(sd.yScale),
        bEnabled(sd.bEnabled), bStopped(sd.bStopped),
        m_container(sd.m_container)
    {

    }

    virtual ~SignalData() {}

    SignalData & operator = (const SignalData & sd)
    {
        program = sd.program;
        color = sd.color;
        xOffset = sd.xOffset;
        yOffset = sd.yOffset;
        xScale = sd.xScale;
        yScale = sd.yScale;
        bEnabled = sd.bEnabled;
        bStopped = sd.bStopped;
        m_container = sd.m_container;

        return *this;
    }

public:
    QMutex mutex;
    QScriptProgram program;
    QColor color;

    int xOffset;
    int yOffset;
    int xScale;
    int yScale;

    bool bEnabled;
    bool bStopped;

    void append(const T & t)
    {
        QMutexLocker lock(&mutex);

        m_container.push_back(t);
        m_container.pop_front();
    }

    int size() const
    {
        return m_container.size();
    }

    void reset(int count, const T & t = T())
    {
        m_container.clear();

        for(int i = 0; i < count; ++i)
            m_container.append(t);
        //for(auto it = m_container.begin(); it != m_container.end(); ++it)
        //    *it = T();
    }

    const T & operator[] ( int i ) const
    {
        return m_container[i];
    }

    T & operator[] ( int i )
    {
        return m_container[i];
    }

    const container_type& getContainer() const { return m_container; }

protected:
    container_type m_container;
};

#endif // SIGNALDATA_H
